<?php
/**
 * Created by PhpStorm.
 * Admininfo: Administrator
 * Date: 2017/12/5 0005
 * Time: 17:42
 */
namespace app\behavior;//注意应用或模块的不同命名空间
use app\admin\model\Discount;
use app\admin\model\Mydiscount;
use think\Session;
class Test{
    use \traits\controller\Jump;//类里面引入jump;类
     public function CheckAuth(){
        $request=  \think\Request::instance();
        $controller_name = $request->controller();
        if($controller_name!="Index"){
            if(Session::get('userinfo.id')==""){
                 return jumpinfo('您尚未登录,请先去登录！',url('Index/login'),2,2);
                 exit();
            }
        }
    }

    /**
     * @throws \think\exception\DbException
     * 钩子去处理优惠券的状态
     */
    public function editdiscount(){
         $Discount=new Discount();
         $Mydiscount=new Mydiscount();
         $dislist=Discount::all(['d_status'=>1]);
         foreach ($dislist as $k=>$v){
             if($v['endtime']<time()){
                 $alllist[]=$v['id'];
             }
         }
         if(count($alllist)){
             //先处理大平台的发布优惠券的状态！
             $Discount->save(['d_status' => 2],function($query) use($alllist){
                 $query->where('id', 'in', $alllist);
             });
             //* 在处理用户优惠券里含有这个优惠券的！
             $Mydiscount->save(['use_status'=>3],function ($query) use($alllist){
                 $query->where('discount_id', 'in', $alllist)
                       ->where('use_status',1);
             });
         }
    }
}
